﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;
using FinWeb3.Models.Repository;
using FinWeb3.Models.Helpers;

namespace FinWeb3.Models
{
    public class SnapshotManager
    {
        private IIdentity user;
        private IRepository<Snapshot> snapshotRepository;
        private IRepository<SnapshotLine> snapshotLineRepository;

        public SnapshotManager(IRepository<Snapshot> snapshotRepository, IRepository<SnapshotLine> snapshotLineRepository, IIdentity user)
        {
            this.user = user;
            this.snapshotRepository = snapshotRepository;
            this.snapshotLineRepository = snapshotLineRepository;
        }

        public void Save(StatementManager statementManager, List<Bill> bills, List<Bank> banks, DateTime fromDate, DateTime toDate, string description)
        {
            var snapshot = new Snapshot()
            {
                Description = description,
                FromDate = fromDate,
                ToDate = toDate,
                UserName = user.Name,
            };
            snapshotRepository.Save(snapshot, user);

            foreach (var account in banks)
            {
                var balance = statementManager.GetBalance(account.Id);
                var snapshotLine = new SnapshotLine()
                {
                    BankId = account.Id,
                    Day = DateTime.Today,
                    Description = "Saldo em C/C em " + DateTime.Today.ToString("dd/MM/yyyy"),
                    HasDetailList = false,
                    ReceiveBill = false,
                    SnapshotId = snapshot.Id,
                    UserName = user.Name,
                    Value = balance,
                };

                snapshotLineRepository.Save(snapshotLine, user);
            }

            foreach (var bill in bills)
            {
                var snapshotLine = new SnapshotLine()
                {
                    BankId = bill.BankId,
                    Category = bill.Category,
                    Day = bill.Day,
                    Description = bill.Description,
                    HasDetailList = bill.HasDetailList,
                    OriginalId = bill.Id,
                    ReceiveBill = bill.ReceiveBill,
                    SnapshotId = snapshot.Id,
                    UserName = user.Name,
                    Value = bill.Value,
                };

                snapshotLineRepository.Save(snapshotLine, user);

                if (bill.HasDetailList)
                {
                    foreach (var detail in bill.DetailLines)
                    {
                        var snapshotDetailLine = new SnapshotLine()
                        {
                            BankId = detail.BankId,
                            ParentBillId = detail.ParentBillId,
                            Category = detail.Category,
                            Day = detail.Day,
                            Description = detail.Description,
                            HasDetailList = detail.HasDetailList,
                            OriginalId = detail.Id,
                            ReceiveBill = detail.ReceiveBill,
                            SnapshotId = snapshot.Id,
                            UserName = user.Name,
                            Value = detail.Value,
                        };

                        snapshotLineRepository.Save(snapshotDetailLine, user);
                    }
                }
            }
        }

        public Snapshot Get(int id)
        {
            var snapshot = snapshotRepository.Get(id, user);

            if (snapshot != null)
            {
                snapshot.Lines = snapshotLineRepository.GetAll(user).Where(x => x.SnapshotId == id).ToList();
            }

            return snapshot;
        }

        public List<Snapshot> GetAll()
        {
            return snapshotRepository.GetAll(user).ToList();
        }

        public void Delete(int id)
        {
            var snapshot = Get(id);

            if (snapshot != null)
            {
                var lines = snapshotLineRepository.GetAll(user).Where(x => x.SnapshotId == id);

                foreach (var line in lines)
                {
                    snapshotLineRepository.Delete(line, user);
                }

                snapshotRepository.Delete(snapshot, user);
            }
        }
    }
}
